import { Service } from "typedi";
import { ROLE_ADDRESS } from "config/config";
import { Platform, Organ, LastSyncState } from "client/configVo";
import axios from "axios";
import { REPO_ERR, RES_INFO } from "config/const";
import { HttpParams } from "client/configVo";

export const REPO_CONFIG_INTERFACE = "repoConfigInterface";

export interface repoConfigInterface {
  getOrgan(opts: HttpParams): Promise<Organ[] | any>; // 获取所属组织
  accStructure(opts: HttpParams): Promise<any>;
}

@Service(REPO_CONFIG_INTERFACE)
class repo implements repoConfigInterface {
  // 获取所属组织
  getOrgan(opts: HttpParams): Promise<Organ[] | any> {
    const { request, response, userInfo } = opts;
    return new Promise<Platform[] | any>(async (resolve, reject) => {
      const url = `${ROLE_ADDRESS}/organization/get_structure`;
      try {
        const res: any = await axios({
          url,
          method: "GET",
        });
        const list = res?.data?.data || [];
        list.forEach((item: any) => {
          item.value = -item.value;
          delete item.children;
        });
        if (res.data.no != 0) {
          resolve(REPO_ERR(res.data, "获取所属组织"));
        } else {
          resolve(RES_INFO({ data: list }));
        }
      } catch (e) {
        resolve(REPO_ERR(e, "获取所属组织"));
      }
    });
  }

  // 获取所有人员
  accStructure(opts: HttpParams): Promise<Organ[] | any> {
    const { request, response, userInfo } = opts;
    return new Promise<Platform[] | any>(async (resolve, reject) => {
      const url = `${ROLE_ADDRESS}/organization/get_structure`;
      try {
        const res: any = await axios({
          url,
          method: "GET",
        });
        const list = res?.data?.data || [];
        if (res.data.no != 0) {
          resolve(REPO_ERR(res.data, "获取所有人员"));
        } else {
          resolve(RES_INFO({ data: list }));
        }
      } catch (e) {
        resolve(REPO_ERR(e, "获取所有人员"));
      }
    });
  }
}
